package com.nerve.android.cloudoffice.service.data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.nerve.util.DateUtil;

import android.annotation.SuppressLint;

import com.alibaba.fastjson.JSON;
import com.nerve.android.cloudoffice.common.Const;
import com.nerve.android.cloudoffice.common.Reply;
import com.nerve.android.cloudoffice.common.Resource;
import com.nerve.android.cloudoffice.domain.Task;
import com.nerve.android.cloudoffice.domain.TrashApply;
import com.nerve.android.cloudoffice.service.NetService;

/**
 * @project: Cloudoffice
 * @file: TaskData.java
 * @package: com.nerve.android.cloudoffice.service.data
 * @description:
 *	
 * @author: 集成显卡	1053214511@qq.com
				IBM GDC, zengxm@cn.ibm.com
 * @date&time: Aug 16, 2013
 * @change log:
 */
public final class TaskData {
	private static final String URL = Const.HOST + "task/";
	
	public static final int DAY = 0;
	public static final int WEEK = 1;
	public static final int MONTH = 2;
	public static final int YEAR = 3;
	
	public static final int DELETE = 2;
	
	@SuppressLint("UseSparseArrays")
	private static Map<Integer, List<Task>> map = new HashMap<Integer, List<Task>>();
	
	/**
	 * @method name: get
	 * @description: 获取指定职员的任务列表
	 * 	参数说明：
	 * 	workerid 是职员id
	 * 	id 为部门id
	 * 	type 为选择类型
	 * 
	 * @return type: Reply
	 *
	 * @param userid
	 * @param type
	 * @return
	 */
	@SuppressLint("DefaultLocale")
	public static Reply get(int userid, int departmentId, int type){
		List<Task> tasks = map.get(type);
		Reply re = new Reply();
		if(tasks == null){
			re = NetService.get(
					String.format(
						URL + "list.htm?workerId=%1$d&id=%2$d&range=%3$s", 
						userid,
						departmentId,
						Resource.Task.RANGE[type]
						)
					);
			if(!re.isError()){
				try{
					tasks = JSON.parseArray(re.getMessage().toString(), Task.class);
				}catch(Exception e){
					e.printStackTrace(System.out);
				}
			}
			
			if(tasks == null)
				tasks = new ArrayList<Task>();
			
			map.put(type, tasks);
		}
		
		re.setMessage(tasks);
		return re;
	}
	
	/**
	 * @method name: update
	 * @description: 更新一个任务
	 * @return type: Reply
	 *
	 * @param id
	 * @param type
	 * @param value
	 * @return
	 */
	public static Reply update(int id, int type, String value){
		Map<String, String> pp = new HashMap<String, String>();
		pp.put("id", id+"");
		pp.put("updateType", type+"");
		pp.put("updateValue", value);
		
		Reply re = NetService.post(URL + "update.htm", pp);
		
		return re;
	}
	
	public static Reply updateTask(Task task){
		Map<String, String> pp = new HashMap<String, String>();
		pp.put("task.id", task.getId()+"");
		pp.put("task.level", task.getLevel()+"");
		pp.put("task.message", task.getMessage());
		pp.put("task.status", task.getStatus()+"");
		pp.put("task.remark", task.getRemark());
		pp.put("task.tag", task.getTag());
		pp.put("task.project", task.getProject());
		
		
		Reply re = NetService.post(URL + "updateBatch.htm", pp);
		
		return re;
	}
	
	/**
	 * @method name: add
	 * @description: 增加一个任务
	 * @return type: Reply
	 *
	 * @param t
	 * @return
	 */
	public static Reply add(Task t){
		Map<String, String> pp = new HashMap<String, String>();
		pp.put("task.title", t.getTitle());
		pp.put("workerId", t.getWorkUser());
		pp.put("startDate", DateUtil.toString(t.getStartDate()));
		pp.put("endDate", DateUtil.toString(t.getEndDate()));
		pp.put("task.level", t.getLevel() + "");
		pp.put("task.location", t.getLoaction()+"");
		pp.put("task.remark", t.getRemark());
		pp.put("task.tag", t.getTag());
		pp.put("task.project", t.getProject());
		
		Reply re = NetService.post(URL + "add.htm", pp);
		
		if(re.isError()){
			t.setId(Integer.valueOf(re.getMessage().toString()));
		}
		
		return re;
	}
	
	public static void clearData(){
		map.clear();
	}
	
	public static Reply getTrashApply(){
		Reply re = NetService.get(URL + "applyList.htm");
		
		List<TrashApply> list = new ArrayList<TrashApply>();
		
		if(!re.isError()){
			try{
				list = JSON.parseArray(re.getMessage().toString(), TrashApply.class);
			}catch(Exception e){
				e.printStackTrace(System.out);
			}
		}
		
		re.setMessage(list);
		return re;
	}
	
	public static Reply passApply(int id){
		Reply re = NetService.get(URL + "passTrashApply.htm?id="+id);
		
		return re;
	}
}